<template>
  <el-dialog v-model="dialogVisible" :title="title" width="500px" :append-to-body="true" :before-close="handleClose">
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm" status-icon>
    <el-form-item label="车牌号" prop="carNumber">
        <el-input placeholder="请输入" v-model="ruleForm.carNumber" />
 
      </el-form-item>
      <el-form-item label="绑定仓库" prop="warehouseId">
        <el-select v-model="ruleForm.warehouseId" placeholder="请选择" style="width: 100%">
          <el-option v-for="(item, index) in options" :key="index" :label="item.warehouseName" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input placeholder="请输入" type="textarea" show-word-limit maxlength="140" v-model="ruleForm.remark" />
 
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleCloses(ruleFormRef)">取消</el-button>
        <el-button type="primary" @click="submit(ruleFormRef)">
          确 认
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage, FormInstance } from 'element-plus'
import { REQUEST } from '@/api/request'
const ruleFormRef = ref<FormInstance>()
const title = ref('')
const dialogVisible = ref(false)
const emits = defineEmits(['ok'])
const ruleForm = ref({
  id: '',
  warehouseId: '',
  remark:'',
  carNumber:''
})
const options = ref([])
const rules = ref({
  warehouseId: [{ required: true, message: '请选择仓库', trigger: 'change' }],
  carNumber: [{ required: true, message: '请输入车牌号', trigger: 'change' }]
})
const getData = () => {
  options.value = [
    {
      id: 1,
      warehouseName: '北京仓'
    },
    {
      id: 2,
      warehouseName: '上海仓'
    }
  ]
}
const openModal = (t: any, e: any) => {
  getData()
  ruleForm.value = {
    id: '',
    warehouseId: '',
    remark:'',
    carNumber:''
  }
  dialogVisible.value = true
  title.value = t
  if (e) {
    ruleForm.value = e
  }
}
const handleClose = () => {
  dialogVisible.value = false
  emits('ok')
}
const handleCloses = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  emits('ok')
  dialogVisible.value = false
}
const submit = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      if (title.value === '新增车辆') {

        // REQUEST.POST('USERADD', ruleForm.value).then((res: any) => {
        //   if (res.status === 200) {
        //     ElMessage({
        //       type: 'success',
        //       message: '添加成功！'
        //     })
        //     formEl.resetFields()
        //     dialogVisible.value = false
        //     emits('ok')
        //   } else {
        //     ElMessage({
        //       type: 'warning',
        //       message: res.message
        //     })
        //   }
        // })
      } else {
        // REQUEST.POST('USERSETTING', ruleForm.value).then((res: any) => {
        //   if (res.status === 200) {
        //     ElMessage({
        //       type: 'success',
        //       message: '设置成功！'
        //     })
        //     dialogVisible.value = false
        //     formEl.resetFields()
        //     emits('ok')
        //   } else {
        //     ElMessage({
        //       type: 'warning',
        //       message: res.message
        //     })
        //   }
        // })

      }
    }
  })
}
defineExpose({ openModal })
</script>

<style lang="" scoped></style>
